package com.lincoln.service;

import com.lincoln.bean.ApiCode;
import com.lincoln.bean.ApiCodeException;
import com.lincoln.entity.role.DbRole;
import com.lincoln.repository.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class RoleService extends BaseService<DbRole, RoleRepository> {
    @Autowired
    PermissionService permissionService;

    /**
     * 建立分页排序请求
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public PageRequest buildPageRequest(int pageNo, int pageSize, Sort.Order... orders) {
        Sort sort = Sort.by(orders);
        return PageRequest.of(pageNo, pageSize, sort);
    }

    @Override
    public Predicate createPredicate(Root<DbRole> root, CriteriaBuilder cb, Map<String, Object> params) {
        Predicate result = cb.and();
        if (params.get("name") != null) {
            Predicate p = cb.equal(root.get("name").as(String.class), params.get("name"));
            result = cb.and(result, p);
        }
        return result;
    }

    public DbRole save(String name, String permissions) throws ApiCodeException {
        DbRole role = findByUniqueForOne("name", name);
        if (role != null) {
            throw new ApiCodeException(ApiCode.build(ApiCode.FAIL, "role is already existed!"));
        }
        role = new DbRole();
        role.setName(name);
        role.setPermissions(permissionService.findByIds(permissions));
        return super.save(role);
    }

    public DbRole update(long id, String name, String permissions) throws ApiCodeException {
        DbRole role = findById(id);
        if (role == null) {
            throw new ApiCodeException(10006);
        }
        if ((null == name || "".equals(name)) && !name.equals(role.getName())) {
            DbRole r = findByUniqueForOne("name", name);
            if (r != null) {
                throw new ApiCodeException(ApiCode.build(ApiCode.FAIL, "role is already existed!"));
            }
            role.setName(name);
        }
        if (null == permissions || "".equals(permissions)) {
            role.setPermissions(permissionService.findByIds(permissions));
        }
        return super.save(role);
    }

    /**
     * 通过等级id或名称查询等级
     *
     * @param idOrName
     * @return
     */
    public DbRole findByIdOrName(String idOrName) {
        DbRole role = null;
        try {
            long id = Long.parseLong(idOrName);
            role = findById(id);
            if (role == null) {
                throw new Exception("可能不是id,在这抛个异常让下面catch");
            }
        } catch (Exception e) {
            role = findByUniqueForOne("name", idOrName);
        }
        return role;
    }

}
